package dynamic.subarrysubstring;

/**
 * @Classname : MaximumProductSubarray
 * @Description : 152. 乘积最大子数组
 * https://leetcode.cn/problems/maximum-product-subarray/
 * @Author : cty
 * @Date 2023/4/1 22:54
 */


public class MaximumProductSubarray {
    public int maxProduct(int[] nums) {
        int ans = Integer.MIN_VALUE, max = 1, min = 1;

        for (int i = 1; i <= nums.length; i++) {
            int num = nums[i - 1];
            int v1 = max <= 0 ? num : num * max;
            int v2 = min > 0 ? num : num * min;
            if (num >= 0) {
                max = v1;
                min = v2;
            } else {
                max = v2;
                min = v1;
            }
            ans = Math.max(ans, max);
        }
        return ans;
    }
}
